package com.lyh.hkcloud.util.base;

import android.util.Log;
import android.widget.EditText;
import android.widget.TextView;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

public class StringUtils {
    public StringUtils() {
    }

    public static String ifEmptyEelseGet(String str,String elseValue){
        if(isEmpty(str)){
            return elseValue;
        }
        return str;
    }

    public static boolean isEmpty(String str) {
        return str == null || str.trim().length() < 1 || "null".equals(str.trim());
    }

    public static boolean isNotEmpty(String str) {
        return !isEmpty(str);
    }

    public static boolean isEditEmpty(EditText text) {
        return text == null || "".equals(text.getText().toString().trim());
    }

    public static String listToString(List list, String separator) {
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < list.size(); ++i) {
            sb.append(list.get(i)).append(separator);
        }

        return separator.equals("") ? sb.toString() : sb.toString().substring(0, sb.toString().length() - 1);
    }

    public static List<String> stringToList(String string, String separator) {
        new StringBuilder();
        if (isEmpty(string)) {
            return null;
        } else {
            String[] substrs = string.split(separator);
            if ("|".equals(separator)) {
                substrs = string.split("\\|");
            }

            List<String> substrList = new ArrayList();
            if (substrs != null) {
                String[] var8 = substrs;
                int var7 = substrs.length;

                for (int var6 = 0; var6 < var7; ++var6) {
                    String sbustr = var8[var6];
                    substrList.add(sbustr);
                }
            }

            return substrList;
        }
    }

    public static String collectToString(Collection<String> collect, String separator) {
        List<String> list = new ArrayList();
        Iterator var4 = collect.iterator();

        while (var4.hasNext()) {
            String id = (String) var4.next();
            list.add(id);
        }

        return listToString(list, separator);
    }

    public static String toString(Object value, String defaultValue) {
        if (value == null) {
            return defaultValue;
        }
        return value.toString();
    }

    public static int toInt(Object value, int defaultValue) {
        if (value == null || isEmpty(value.toString())) {
            return defaultValue;
        }
        String s = toString(value, "0").trim();
        if (s.indexOf(".") > 0) {
            s = s.substring(0, s.indexOf("."));
        }
        return Integer.parseInt(s);
    }

    public static boolean toBoolean(Object value, boolean defaultValue) {
        if (value == null) {
            return defaultValue;
        }
        return Boolean.parseBoolean(toString(value, "false"));
    }

    public static String getNotEmpty(String defaultValue, String... values) {
        for (String value : values) {
            if (isNotEmpty(value)) {
                return value;
            }
        }
        return defaultValue;
    }

    public static float toFloat(Object value, float defaultValue) {
        if (value == null || isEmpty(value.toString())) {
            return defaultValue;
        }
        String v = toString(value, defaultValue + "");
        if ("".equals(v)) {
            v = defaultValue + "";
        }
        return Float.parseFloat(v);
    }

    public static String clearLeftZero(String value) {
        return clearLeft(value, "0");
    }

    public static String clearLeft(String value, String c) {
        return value.replaceFirst("^" + c + "*", "");
    }

    public static String toLeft(String value, int index) {
        if (isEmpty(value) || value.length() <= index) {
            return value;
        }
        return value.substring(0, index);
    }

    public static String toRight(String value, int index) {
        if (isEmpty(value) || value.length() <= index) {
            return value;
        }
        return value.substring(value.length() - index);
    }

    public static Boolean responseResults(JSONObject response) throws JSONException {
        Log.v("StringUtils-response",response.toString());
        if ("0000".equals(response.get("code"))) {
            return true;
        }
        return false;
    }
}
